﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAO;
using WebComputers.Class;
using System.Web.Services;


namespace WebComputers
{
    public partial class SanPham : System.Web.UI.Page
    {
        const int slHienThi = 3;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillData();
            }
        }

        private void FillData()
        {
            try
            {
                rptTenLoai.DataSource = LoaiHangHoaDAO.SelectAllLinq();
                rptTenLoai.DataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void rptTenLoai_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            try
            {
                RepeaterItem item = e.Item;
                if ((item.ItemType == ListItemType.Item) || (item.ItemType == ListItemType.AlternatingItem))
                {
                    Repeater rptsanPham = item.FindControl("rptSanPhamCon") as Repeater;
                    //int id = Convert.ToInt32(DataBinder.Eval(item.DataItem,"iLoaiHangHoa_ID"));                    
                    dynamic obj = item.DataItem as dynamic;// with linq
                    int id = obj.iLoaiHangHoa_ID;
                    rptsanPham.DataSource = DanhMucHangHoaDAO.SelectByLoaiSanPhamLinq(id, 1, slHienThi, ref id);
                    rptsanPham.DataBind();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected string FillImage(string fileName)
        {
            return DAHelper.FillImage(fileName);
        }

        [WebMethod(EnableSession = true)]
        public static string AddToCart(int id)
        {
            return DAHelper.AddToCart(id);
        }
    }
}